from aip import AipNlp


class BaiDuApiNPL:
    def __init__(self,config):
        self.config = config
        self.APP_ID = self.config['baidu_NPL_api']['app_id']
        self.API_KEY = self.config['baidu_NPL_api']['api_key']
        self.SECRET_KEY = self.config['baidu_NPL_api']['secret_key']
        self.client = AipNlp(self.APP_ID, self.API_KEY, self.SECRET_KEY)

    def get_WordSeg(self, text):
        try:
            result = self.client.lexer(text=text, options={})
            if 'error_code' in result:
                raise Exception('BaiDuApiNPL error: ',result)
            # 处理为数组
            words = []
            items = result['items']
            for item in items:
                word = item['basic_words']
                for sub_word in word:
                    words.append(sub_word)
            return words
        except Exception as e:
            raise e
        
    



